#include "stdafx.h"

#ifndef lint
static const char yysccsid[] = "@(#)yaccpar	1.9 (Berkeley) 02/21/93";
#endif

#define YYBYACC 1
#define YYMAJOR 1
#define YYMINOR 9

#define YYEMPTY        (-1)
#define yyclearin      (yychar = YYEMPTY)
#define yyerrok        (yyerrflag = 0)
#define YYRECOVERING() (yyerrflag != 0)

#define YYPREFIX "yy"

#define YYPURE 1

#line 2 "sql.y"

#include "token.h"
#include "node.h"
#include "Expression.h"
#include "ParserContext.h"

#define YYPARSE_PARAM_TYPE ParserContext&
#define YYPARSE_PARAM context
#define YYLEX_PARAM_TYPE ParserContext&
#define YYLEX_PARAM context

typedef union
{
    const Token * token;
    Clause * clause;
    Statement * statement;
    OrderByItem * orderbyitem;
    std::vector<OrderByItem *> * orderbyitems;

    Expression * expression;
    ArithmeticExpression * arithexpression;
    BooleanExpression * boolexpression;
    std::vector<Expression *> * expressions;
    std::vector<Column *> *columns;
    ColumnExpression * columnexpression;
    Column * column;
} YYSTYPE;

#line 47 "yyparser.cpp"

/* compatibility with bison */
#ifdef YYPARSE_PARAM
/* compatibility with FreeBSD */
# ifdef YYPARSE_PARAM_TYPE
#  define YYPARSE_DECL() yyparse(YYPARSE_PARAM_TYPE YYPARSE_PARAM)
# else
#  define YYPARSE_DECL() yyparse(void *YYPARSE_PARAM)
# endif
#else
# define YYPARSE_DECL() yyparse(void)
#endif

/* Parameters sent to lex. */
#ifdef YYLEX_PARAM
# ifdef YYLEX_PARAM_TYPE
#  define YYLEX_DECL() yylex(YYSTYPE *yylval, YYLEX_PARAM_TYPE YYLEX_PARAM)
# else
#  define YYLEX_DECL() yylex(YYSTYPE *yylval, void * YYLEX_PARAM)
# endif
# define YYLEX yylex(&yylval, YYLEX_PARAM)
#else
# define YYLEX_DECL() yylex(YYSTYPE *yylval)
# define YYLEX yylex(&yylval)
#endif

/* Parameters sent to yyerror. */
#ifndef YYERROR_DECL
#define YYERROR_DECL() yyerror(const char *s)
#endif
#ifndef YYERROR_CALL
#define YYERROR_CALL(msg) yyerror(msg)
#endif

extern int YYPARSE_DECL();

#define T_SQL_SELECT 257
#define T_SQL_WHERE 258
#define T_SQL_FROM 259
#define T_SQL_LIKE 260
#define T_SQL_TOP 261
#define T_SQL_ORDER 262
#define T_SQL_BY 263
#define T_SQL_ASC 264
#define T_SQL_DESC 265
#define T_SQL_DISTINCT 266
#define T_SQL_AS 267
#define T_SQL_AND 268
#define T_SQL_OR 269
#define T_SQL_COUNT 270
#define T_SQL_MAX 271
#define T_SQL_MIN 272
#define T_STRING_LITERAL 273
#define T_NUMBER_LITERAL 274
#define T_IDENTIFIER 275
#define T_ASSIGN 276
#define T_COMMAS 277
#define T_SEMICOLON 278
#define T_ASTERISK 279
#define T_LPAREN 280
#define T_RPAREN 281
#define T_ADD 282
#define T_MINUS 283
#define T_DIVIDE 284
#define T_EQUAL 285
#define T_NOT_EQUAL 286
#define T_LESS 287
#define T_GREATER 288
#define T_LESS_OR_EQUAL 289
#define T_GREATER_OR_EQUAL 290
#define LEXERROR 291
#define YYERRCODE 256
static const short yylhs[] = {                           -1,
    0,    0,    1,    2,    2,    3,    3,    4,    4,    6,
    6,    6,    5,    5,   15,   15,   16,   16,   17,   17,
   17,   17,   17,    7,    7,   12,   12,   12,   12,   12,
   12,   12,   12,   12,   12,   11,   11,   11,   11,   11,
   11,   11,   11,   11,   14,   14,   14,   13,   13,    8,
    8,   10,   10,    9,    9,    9,
};
static const short yylen[] = {                            2,
    2,    3,    1,    7,    8,    0,    2,    0,    2,    2,
    2,    4,    1,    1,    1,    3,    1,    3,    1,    1,
    4,    4,    4,    0,    2,    3,    3,    3,    3,    3,
    3,    3,    3,    3,    3,    1,    1,    1,    3,    3,
    3,    3,    3,    4,    0,    1,    3,    1,    1,    0,
    3,    1,    3,    1,    2,    2,
};
static const short yydefred[] = {                         0,
    0,    0,    0,    3,    0,    7,    0,    1,    0,    2,
    0,    0,    9,    0,    0,    0,    0,   37,   38,    0,
   14,    0,    0,    0,    0,    0,   15,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0,    0,    0,    0,    0,   29,    0,    0,   46,
    0,   39,   26,   10,    0,    0,    0,    0,    0,   42,
    0,    0,   43,    0,    0,    0,    0,    0,    0,    0,
    0,   28,   16,   18,    0,   21,   22,   23,    0,   44,
    0,    0,    0,    4,    0,    5,   47,   12,    0,    0,
   52,    0,   55,   56,    0,   53,
};
static const short yydgoto[] = {                          2,
    3,    4,    5,   12,   23,   38,   67,   94,  101,  102,
   24,   25,   60,   61,   26,   27,   28,
};
static const short yysindex[] = {                      -232,
 -231, -232, -210,    0, -187,    0, -203,    0, -162,    0,
 -172,   -5,    0,   41, -169, -151, -149,    0,    0, -247,
    0,   55, -126,   20, -153, -129,    0, -118, -126,   52,
   52,   52, -122,   55,    8, -265, -238, -103,   63,   63,
   63,   63,   63,   63,   63,   63,   63,   63,   55,   55,
   52, -119, -103, -124, -121, -116,    0,   20, -153,    0,
 -266,    0,    0,    0, -108,   55,  -95, -107,   63,    0,
 -278, -278,    0, -205, -205, -205, -205, -205, -205,   20,
  -94,    0,    0,    0,  -95,    0,    0,    0,   55,    0,
 -101, -153,  -87,    0, -274,    0,    0,    0,  -93, -137,
    0, -100,    0,    0,  -93,    0,
};
static const short yyrindex[] = {                       -73,
    0,  -73,    0,    0,    0,    0,    0,    0,    6,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0, -147,
    0,    0,    0, -171, -109,  -74,    0, -194,    0,    0,
    0,    0,    0, -239,    0,    0,    0, -214,    0,    0,
    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,
    0,    0, -214,    0,    0,    0,    0, -201, -180,    0,
    0,    0,    0,    0, -226,    0,  -89, -228,    0,    0,
 -196, -164,    0, -115,  -98,  -81,  -64,  -47,  -30,    0,
  -18,    0,    0,    0,  -89,    0,    0,    0,    0,    0,
    0, -178,    0,    0,    0,    0,    0,    0,    0, -132,
    0,  -88,    0,    0,    0,    0,
};
static const short yygindex[] = {                         0,
  189,    0,    0,    0,  178,  164,  141,  114,   96,    0,
  -22,  -20,  113,    0,    0,  155,   77,
};
#define YYTABLESIZE 343
static const short yytable[] = {                         35,
   39,   36,   49,   50,   39,   42,   62,   40,   41,   42,
   89,   58,   33,   59,   90,   63,   70,   71,   72,   73,
   74,   75,   76,   77,   78,   79,   80,   80,   81,   82,
   36,   11,   34,   36,   64,   11,   65,   45,   36,   36,
   36,   45,    1,   80,    6,   92,   95,   24,   36,   36,
   36,   11,   36,   36,   36,   36,   36,   36,   36,   36,
   36,   36,   40,   24,   17,   40,   58,    8,   59,    9,
   40,   40,   40,   39,   10,   48,   40,   41,   42,   48,
   40,   40,   17,   25,   40,   40,   40,   20,   40,   40,
   40,   40,   40,   40,   41,   20,   49,   41,   11,   25,
   49,   13,   41,   41,   41,   20,   54,   55,   56,   20,
   30,   36,   41,   41,   49,   50,   41,   41,   41,   36,
   41,   41,   41,   41,   41,   41,  103,  104,   31,   36,
   32,   36,   37,   36,   36,   36,   36,   36,   36,   36,
   36,   36,   36,   30,   54,   54,   30,   51,   52,   19,
   57,   30,   30,   30,   66,   84,   86,   19,   91,   87,
   31,   30,   30,   31,   88,   30,   93,   19,   31,   31,
   31,   19,   34,   98,   50,   99,  105,   32,   31,   31,
   32,  100,   31,    6,   13,   32,   32,   32,   50,   51,
    7,   29,   53,   85,   33,   32,   32,   33,   96,   32,
  106,   97,   33,   33,   33,   83,    0,    0,    0,    0,
    0,   34,   33,   33,   34,    0,   33,    0,    0,   34,
   34,   34,    0,    0,    0,    0,    0,    0,   35,   34,
   34,   35,    0,   34,    0,    0,   35,   35,   35,    0,
   27,    0,    0,   27,    0,    0,   35,   35,   27,   27,
   35,    0,    0,    0,    0,    0,    0,    0,   27,   27,
   14,    0,   27,    0,   15,   16,   17,   18,   19,   20,
    0,    8,    0,   21,   22,    8,    8,    8,    8,    8,
    8,    0,    0,    0,    8,    8,   39,    0,   62,   40,
   41,   42,   43,   44,   45,   46,   47,   48,   39,    0,
    0,   40,   41,   42,   43,   44,   45,   46,   47,   48,
   15,   16,   17,   18,   19,   20,    0,    0,    0,   21,
   22,   15,   16,   17,   18,   19,   20,   18,   19,   20,
    0,   22,    0,    0,   22,   18,   19,   68,    0,    0,
    0,    0,   69,
};
static const short yycheck[] = {                         22,
  279,   22,  268,  269,  279,  284,  281,  282,  283,  284,
  277,   34,  260,   34,  281,  281,   39,   40,   41,   42,
   43,   44,   45,   46,   47,   48,   49,   50,   49,   50,
  259,  258,  280,  262,  273,  262,  275,  277,  267,  268,
  269,  281,  275,   66,  276,   66,   69,  262,  277,  278,
  279,  278,  281,  282,  283,  284,  285,  286,  287,  288,
  289,  290,  259,  278,  259,  262,   89,  278,   89,  257,
  267,  268,  269,  279,  278,  277,  282,  283,  284,  281,
  277,  278,  277,  262,  281,  282,  283,  259,  285,  286,
  287,  288,  289,  290,  259,  267,  277,  262,  261,  278,
  281,  274,  267,  268,  269,  277,   30,   31,   32,  281,
  280,  259,  277,  278,  268,  269,  281,  282,  283,  267,
  285,  286,  287,  288,  289,  290,  264,  265,  280,  277,
  280,  279,  259,  281,  282,  283,  284,  285,  286,  287,
  288,  289,  290,  259,  277,  278,  262,  277,  267,  259,
  273,  267,  268,  269,  258,  275,  281,  267,  267,  281,
  259,  277,  278,  262,  281,  281,  262,  277,  267,  268,
  269,  281,  280,  275,  269,  263,  277,  259,  277,  278,
  262,  275,  281,  257,  259,  267,  268,  269,  278,  278,
    2,   14,   29,   53,  259,  277,  278,  262,   85,  281,
  105,   89,  267,  268,  269,   51,   -1,   -1,   -1,   -1,
   -1,  259,  277,  278,  262,   -1,  281,   -1,   -1,  267,
  268,  269,   -1,   -1,   -1,   -1,   -1,   -1,  259,  277,
  278,  262,   -1,  281,   -1,   -1,  267,  268,  269,   -1,
  259,   -1,   -1,  262,   -1,   -1,  277,  278,  267,  268,
  281,   -1,   -1,   -1,   -1,   -1,   -1,   -1,  277,  278,
  266,   -1,  281,   -1,  270,  271,  272,  273,  274,  275,
   -1,  266,   -1,  279,  280,  270,  271,  272,  273,  274,
  275,   -1,   -1,   -1,  279,  280,  279,   -1,  281,  282,
  283,  284,  285,  286,  287,  288,  289,  290,  279,   -1,
   -1,  282,  283,  284,  285,  286,  287,  288,  289,  290,
  270,  271,  272,  273,  274,  275,   -1,   -1,   -1,  279,
  280,  270,  271,  272,  273,  274,  275,  273,  274,  275,
   -1,  280,   -1,   -1,  280,  273,  274,  275,   -1,   -1,
   -1,   -1,  280,
};
#define YYFINAL 2
#ifndef YYDEBUG
#define YYDEBUG 1
#endif
#define YYMAXTOKEN 291
#if YYDEBUG
static const char *yyname[] = {

"end-of-file",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"T_SQL_SELECT","T_SQL_WHERE",
"T_SQL_FROM","T_SQL_LIKE","T_SQL_TOP","T_SQL_ORDER","T_SQL_BY","T_SQL_ASC",
"T_SQL_DESC","T_SQL_DISTINCT","T_SQL_AS","T_SQL_AND","T_SQL_OR","T_SQL_COUNT",
"T_SQL_MAX","T_SQL_MIN","T_STRING_LITERAL","T_NUMBER_LITERAL","T_IDENTIFIER",
"T_ASSIGN","T_COMMAS","T_SEMICOLON","T_ASTERISK","T_LPAREN","T_RPAREN","T_ADD",
"T_MINUS","T_DIVIDE","T_EQUAL","T_NOT_EQUAL","T_LESS","T_GREATER",
"T_LESS_OR_EQUAL","T_GREATER_OR_EQUAL","LEXERROR",
};
static const char *yyrule[] = {
"$accept : statements",
"statements : sql_statement T_SEMICOLON",
"statements : statements sql_statement T_SEMICOLON",
"sql_statement : select_statement",
"select_statement : assignment_clause T_SQL_SELECT top_clause select_content_clause from_clause where_clause orderby_clause",
"select_statement : assignment_clause T_SQL_SELECT top_clause T_SQL_DISTINCT select_content_clause from_clause where_clause orderby_clause",
"assignment_clause :",
"assignment_clause : T_IDENTIFIER T_ASSIGN",
"top_clause :",
"top_clause : T_SQL_TOP T_NUMBER_LITERAL",
"from_clause : T_SQL_FROM T_STRING_LITERAL",
"from_clause : T_SQL_FROM T_IDENTIFIER",
"from_clause : T_SQL_FROM T_IDENTIFIER T_SQL_AS T_IDENTIFIER",
"select_content_clause : column_list",
"select_content_clause : T_ASTERISK",
"column_list : column",
"column_list : column_list T_COMMAS column",
"column : column_expr",
"column : column_expr T_SQL_AS T_IDENTIFIER",
"column_expr : bool_expr",
"column_expr : arith_expr",
"column_expr : T_SQL_COUNT T_LPAREN column_expr T_RPAREN",
"column_expr : T_SQL_MAX T_LPAREN column_expr T_RPAREN",
"column_expr : T_SQL_MIN T_LPAREN column_expr T_RPAREN",
"where_clause :",
"where_clause : T_SQL_WHERE bool_expr",
"bool_expr : T_LPAREN bool_expr T_RPAREN",
"bool_expr : bool_expr T_SQL_AND bool_expr",
"bool_expr : bool_expr T_SQL_OR bool_expr",
"bool_expr : T_IDENTIFIER T_SQL_LIKE T_STRING_LITERAL",
"bool_expr : arith_expr T_EQUAL arith_expr",
"bool_expr : arith_expr T_NOT_EQUAL arith_expr",
"bool_expr : arith_expr T_LESS arith_expr",
"bool_expr : arith_expr T_GREATER arith_expr",
"bool_expr : arith_expr T_LESS_OR_EQUAL arith_expr",
"bool_expr : arith_expr T_GREATER_OR_EQUAL arith_expr",
"arith_expr : T_IDENTIFIER",
"arith_expr : T_STRING_LITERAL",
"arith_expr : T_NUMBER_LITERAL",
"arith_expr : T_LPAREN arith_expr T_RPAREN",
"arith_expr : arith_expr T_ADD arith_expr",
"arith_expr : arith_expr T_MINUS arith_expr",
"arith_expr : arith_expr T_ASTERISK arith_expr",
"arith_expr : arith_expr T_DIVIDE arith_expr",
"arith_expr : T_IDENTIFIER T_LPAREN function_parameter_list T_RPAREN",
"function_parameter_list :",
"function_parameter_list : function_parameter",
"function_parameter_list : function_parameter_list T_COMMAS function_parameter",
"function_parameter : arith_expr",
"function_parameter : bool_expr",
"orderby_clause :",
"orderby_clause : T_SQL_ORDER T_SQL_BY orderby_list",
"orderby_list : orderby_item",
"orderby_list : orderby_list T_COMMAS orderby_item",
"orderby_item : T_IDENTIFIER",
"orderby_item : T_IDENTIFIER T_SQL_ASC",
"orderby_item : T_IDENTIFIER T_SQL_DESC",

};
#endif

int      yydebug;
int      yynerrs;

/* define the initial stack-sizes */
#ifdef YYSTACKSIZE
#undef YYMAXDEPTH
#define YYMAXDEPTH  YYSTACKSIZE
#else
#ifdef YYMAXDEPTH
#define YYSTACKSIZE YYMAXDEPTH
#else
#define YYSTACKSIZE 500
#define YYMAXDEPTH  500
#endif
#endif

#define YYINITSTACKSIZE 500

typedef struct {
    unsigned stacksize;
    short    *s_base;
    short    *s_mark;
    short    *s_last;
    YYSTYPE  *l_base;
    YYSTYPE  *l_mark;
} YYSTACKDATA;
#line 231 "sql.y"
 /* start of programs */
int yyerror(const char * msg)
{
  printf("Error: %s\n", msg);
  return -1;
}

int
YYLEX_DECL()
{
    yylval->token = context.NextToken();
    return (yylval->token != NULL) ? yylval->token->GetTokenType(): -1;
}
#line 382 "yyparser.cpp"

#if YYDEBUG
#include <stdio.h>		/* needed for printf */
#endif

#include <stdlib.h>	/* needed for malloc, etc */
#include <string.h>	/* needed for memset */

/* allocate initial stack or double stack size, up to YYMAXDEPTH */
static int yygrowstack(YYSTACKDATA *data)
{
    int i;
    unsigned newsize;
    short *newss;
    YYSTYPE *newvs;

    if ((newsize = data->stacksize) == 0)
        newsize = YYINITSTACKSIZE;
    else if (newsize >= YYMAXDEPTH)
        return -1;
    else if ((newsize *= 2) > YYMAXDEPTH)
        newsize = YYMAXDEPTH;

    i = data->s_mark - data->s_base;
    newss = (short *)realloc(data->s_base, newsize * sizeof(*newss));
    if (newss == 0)
        return -1;

    data->s_base = newss;
    data->s_mark = newss + i;

    newvs = (YYSTYPE *)realloc(data->l_base, newsize * sizeof(*newvs));
    if (newvs == 0)
        return -1;

    data->l_base = newvs;
    data->l_mark = newvs + i;

    data->stacksize = newsize;
    data->s_last = data->s_base + newsize - 1;
    return 0;
}

#if YYPURE || defined(YY_NO_LEAKS)
static void yyfreestack(YYSTACKDATA *data)
{
    free(data->s_base);
    free(data->l_base);
    memset(data, 0, sizeof(*data));
}
#else
#define yyfreestack(data) /* nothing */
#endif

#define YYABORT  goto yyabort
#define YYREJECT goto yyabort
#define YYACCEPT goto yyaccept
#define YYERROR  goto yyerrlab

int
YYPARSE_DECL()
{
    int      yyerrflag;
    int      yychar;
    YYSTYPE  yyval;
    YYSTYPE  yylval;

    /* variables for the parser stack */
    YYSTACKDATA yystack;
    int yym, yyn, yystate;
#if YYDEBUG
    const char *yys;

    if ((yys = getenv("YYDEBUG")) != 0)
    {
        yyn = *yys;
        if (yyn >= '0' && yyn <= '9')
            yydebug = yyn - '0';
    }
#endif

    yynerrs = 0;
    yyerrflag = 0;
    yychar = YYEMPTY;
    yystate = 0;

#if YYPURE
    memset(&yystack, 0, sizeof(yystack));
#endif

    if (yystack.s_base == NULL && yygrowstack(&yystack)) goto yyoverflow;
    yystack.s_mark = yystack.s_base;
    yystack.l_mark = yystack.l_base;
    yystate = 0;
    *yystack.s_mark = 0;

yyloop:
    if ((yyn = yydefred[yystate]) != 0) goto yyreduce;
    if (yychar < 0)
    {
        if ((yychar = YYLEX) < 0) yychar = 0;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, reading %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
    }
    if ((yyn = yysindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: state %d, shifting to state %d\n",
                    YYPREFIX, yystate, yytable[yyn]);
#endif
        if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
        {
            goto yyoverflow;
        }
        yystate = yytable[yyn];
        *++yystack.s_mark = yytable[yyn];
        *++yystack.l_mark = yylval;
        yychar = YYEMPTY;
        if (yyerrflag > 0)  --yyerrflag;
        goto yyloop;
    }
    if ((yyn = yyrindex[yystate]) && (yyn += yychar) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yychar)
    {
        yyn = yytable[yyn];
        goto yyreduce;
    }
    if (yyerrflag) goto yyinrecovery;

    yyerror("syntax error");

    goto yyerrlab;

yyerrlab:
    ++yynerrs;

yyinrecovery:
    if (yyerrflag < 3)
    {
        yyerrflag = 3;
        for (;;)
        {
            if ((yyn = yysindex[*yystack.s_mark]) && (yyn += YYERRCODE) >= 0 &&
                    yyn <= YYTABLESIZE && yycheck[yyn] == YYERRCODE)
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: state %d, error recovery shifting\
 to state %d\n", YYPREFIX, *yystack.s_mark, yytable[yyn]);
#endif
                if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
                {
                    goto yyoverflow;
                }
                yystate = yytable[yyn];
                *++yystack.s_mark = yytable[yyn];
                *++yystack.l_mark = yylval;
                goto yyloop;
            }
            else
            {
#if YYDEBUG
                if (yydebug)
                    printf("%sdebug: error recovery discarding state %d\n",
                            YYPREFIX, *yystack.s_mark);
#endif
                if (yystack.s_mark <= yystack.s_base) goto yyabort;
                --yystack.s_mark;
                --yystack.l_mark;
            }
        }
    }
    else
    {
        if (yychar == 0) goto yyabort;
#if YYDEBUG
        if (yydebug)
        {
            yys = 0;
            if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
            if (!yys) yys = "illegal-symbol";
            printf("%sdebug: state %d, error recovery discards token %d (%s)\n",
                    YYPREFIX, yystate, yychar, yys);
        }
#endif
        yychar = YYEMPTY;
        goto yyloop;
    }

yyreduce:
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: state %d, reducing by rule %d (%s)\n",
                YYPREFIX, yystate, yyn, yyrule[yyn]);
#endif
    yym = yylen[yyn];
    if (yym)
        yyval = yystack.l_mark[1-yym];
    else
        memset(&yyval, 0, sizeof yyval);
    switch (yyn)
    {
case 1:
#line 64 "sql.y"
	{
        context.AddStatement(yystack.l_mark[-1].statement);
    }
break;
case 2:
#line 68 "sql.y"
	{
        context.AddStatement(yystack.l_mark[-1].statement);
    }
break;
case 3:
#line 74 "sql.y"
	{
        yyval.statement = yystack.l_mark[0].statement;
    }
break;
case 4:
#line 80 "sql.y"
	{
        yyval.statement = new SelectStatement(
            (AssignmentClause *) yystack.l_mark[-6].clause, 
            (TopClause *) yystack.l_mark[-4].clause, 
            SelectStatement::NONE_DISTINCT,
            (SelectContentClause *) yystack.l_mark[-3].clause, 
            (FromClause *) yystack.l_mark[-2].clause, 
            (WhereClause *) yystack.l_mark[-1].clause,
            (OrderByClause *) yystack.l_mark[0].clause);
    }
break;
case 5:
#line 91 "sql.y"
	{
        yyval.statement = new SelectStatement(
            (AssignmentClause *) yystack.l_mark[-7].clause, 
            (TopClause *) yystack.l_mark[-5].clause, 
            SelectStatement::DISTINCT,
            (SelectContentClause *) yystack.l_mark[-3].clause, 
            (FromClause *) yystack.l_mark[-2].clause, 
            (WhereClause *) yystack.l_mark[-1].clause,
            (OrderByClause *) yystack.l_mark[0].clause);
    }
break;
case 6:
#line 103 "sql.y"
	{
        yyval.clause = NULL;
    }
break;
case 7:
#line 107 "sql.y"
	{
        yyval.clause = new AssignmentClause(*yystack.l_mark[-1].token);
    }
break;
case 8:
#line 112 "sql.y"
	{
        yyval.clause = NULL;
    }
break;
case 9:
#line 116 "sql.y"
	{
        yyval.clause = new TopClause(*yystack.l_mark[0].token);
    }
break;
case 10:
#line 122 "sql.y"
	{
        yyval.clause = new FromStreamClause(*yystack.l_mark[0].token);
    }
break;
case 11:
#line 126 "sql.y"
	{
        yyval.clause = new FromVariableClause(*yystack.l_mark[0].token);
    }
break;
case 12:
#line 130 "sql.y"
	{
        yyval.clause = new FromVariableWithAliasClause(*yystack.l_mark[-2].token, *yystack.l_mark[0].token);
    }
break;
case 13:
#line 135 "sql.y"
	{ yyval.clause = new SelectContentClause(yystack.l_mark[0].columns); }
break;
case 14:
#line 136 "sql.y"
	{ yyval.clause = new SelectContentClause(NULL); }
break;
case 15:
#line 139 "sql.y"
	{ yyval.columns = new std::vector<Column *>(); yyval.columns->push_back(yystack.l_mark[0].column); }
break;
case 16:
#line 140 "sql.y"
	{ yyval.columns = yystack.l_mark[-2].columns; yyval.columns->push_back(yystack.l_mark[0].column); }
break;
case 17:
#line 143 "sql.y"
	{ yyval.column = new Column( yystack.l_mark[0].columnexpression ); }
break;
case 18:
#line 144 "sql.y"
	{ yyval.column = new ColumnWithAlias(yystack.l_mark[-2].columnexpression, *yystack.l_mark[0].token); }
break;
case 19:
#line 147 "sql.y"
	{ yyval.columnexpression = new ColumnBooleanExpression( yystack.l_mark[0].boolexpression ); }
break;
case 20:
#line 148 "sql.y"
	{ yyval.columnexpression = new ColumnArithmeticExpression( yystack.l_mark[0].arithexpression ); }
break;
case 21:
#line 149 "sql.y"
	{ yyval.columnexpression = new CountExpression( yystack.l_mark[-1].columnexpression ); }
break;
case 22:
#line 150 "sql.y"
	{ yyval.columnexpression = new MaxExpression( yystack.l_mark[-1].columnexpression ); }
break;
case 23:
#line 151 "sql.y"
	{ yyval.columnexpression = new MinExpression( yystack.l_mark[-1].columnexpression ); }
break;
case 24:
#line 155 "sql.y"
	{
        yyval.clause = NULL;
    }
break;
case 25:
#line 158 "sql.y"
	{ yyval.clause = new WhereClause(yystack.l_mark[0].boolexpression); }
break;
case 26:
#line 161 "sql.y"
	{ yyval.boolexpression = new BooleanParenthesisExpression(yystack.l_mark[-1].boolexpression); }
break;
case 27:
#line 162 "sql.y"
	{ yyval.boolexpression = new AndExpression(yystack.l_mark[-2].boolexpression, yystack.l_mark[0].boolexpression); }
break;
case 28:
#line 163 "sql.y"
	{ yyval.boolexpression = new OrExpression(yystack.l_mark[-2].boolexpression, yystack.l_mark[0].boolexpression); }
break;
case 29:
#line 164 "sql.y"
	{ yyval.boolexpression = new LikeExpression(*yystack.l_mark[-2].token, *yystack.l_mark[0].token); }
break;
case 30:
#line 165 "sql.y"
	{ yyval.boolexpression = new EqualExpression(yystack.l_mark[-2].arithexpression, yystack.l_mark[0].arithexpression); }
break;
case 31:
#line 166 "sql.y"
	{ yyval.boolexpression = new NotEqualExpression(yystack.l_mark[-2].arithexpression, yystack.l_mark[0].arithexpression); }
break;
case 32:
#line 167 "sql.y"
	{ yyval.boolexpression = new LessExpression(yystack.l_mark[-2].arithexpression, yystack.l_mark[0].arithexpression); }
break;
case 33:
#line 168 "sql.y"
	{ yyval.boolexpression = new GreaterExpression(yystack.l_mark[-2].arithexpression, yystack.l_mark[0].arithexpression); }
break;
case 34:
#line 169 "sql.y"
	{ yyval.boolexpression = new LessOrEqualExpression(yystack.l_mark[-2].arithexpression, yystack.l_mark[0].arithexpression); }
break;
case 35:
#line 170 "sql.y"
	{ yyval.boolexpression = new GreaterOrEqualExpression(yystack.l_mark[-2].arithexpression, yystack.l_mark[0].arithexpression); }
break;
case 36:
#line 172 "sql.y"
	{ yyval.arithexpression = new ArithmeticVariableExpression(*yystack.l_mark[0].token); }
break;
case 37:
#line 173 "sql.y"
	{ yyval.arithexpression = new ArithmeticStringExpression(*yystack.l_mark[0].token); }
break;
case 38:
#line 174 "sql.y"
	{ yyval.arithexpression = new ArithmeticNumberExpression(*yystack.l_mark[0].token); }
break;
case 39:
#line 176 "sql.y"
	{ yyval.arithexpression = new ArithmeticParenthesisExpression(yystack.l_mark[-1].arithexpression); }
break;
case 40:
#line 178 "sql.y"
	{ yyval.arithexpression = new AddExpression(yystack.l_mark[-2].arithexpression,yystack.l_mark[0].arithexpression); }
break;
case 41:
#line 179 "sql.y"
	{ yyval.arithexpression = new MinusExpression(yystack.l_mark[-2].arithexpression,yystack.l_mark[0].arithexpression); }
break;
case 42:
#line 180 "sql.y"
	{ yyval.arithexpression = new MultipleExpression(yystack.l_mark[-2].arithexpression,yystack.l_mark[0].arithexpression); }
break;
case 43:
#line 181 "sql.y"
	{ yyval.arithexpression = new DivideExpression(yystack.l_mark[-2].arithexpression,yystack.l_mark[0].arithexpression); }
break;
case 44:
#line 183 "sql.y"
	{ yyval.arithexpression = new FunctionExpression(*yystack.l_mark[-3].token, yystack.l_mark[-1].expressions); }
break;
case 45:
#line 186 "sql.y"
	{ yyval.expressions=NULL; }
break;
case 46:
#line 187 "sql.y"
	{ yyval.expressions = new std::vector<Expression *>(); yyval.expressions->push_back(yystack.l_mark[0].expression); }
break;
case 47:
#line 188 "sql.y"
	{ yyval.expressions=yystack.l_mark[-2].expressions; yyval.expressions->push_back(yystack.l_mark[0].expression); }
break;
case 48:
#line 190 "sql.y"
	{ yyval.expression = yystack.l_mark[0].arithexpression; }
break;
case 49:
#line 191 "sql.y"
	{yyval.expression = yystack.l_mark[0].boolexpression; }
break;
case 50:
#line 195 "sql.y"
	{ 
        yyval.clause = NULL; 
    }
break;
case 51:
#line 199 "sql.y"
	{
        yyval.clause = new OrderByClause(yystack.l_mark[0].orderbyitems);
    }
break;
case 52:
#line 205 "sql.y"
	{
        yyval.orderbyitems = new std::vector<OrderByItem *>();
        yyval.orderbyitems->push_back(yystack.l_mark[0].orderbyitem);

    }
break;
case 53:
#line 211 "sql.y"
	{
        yystack.l_mark[-2].orderbyitems->push_back(yystack.l_mark[0].orderbyitem);
        yyval.orderbyitems = yystack.l_mark[-2].orderbyitems;
    }
break;
case 54:
#line 218 "sql.y"
	{
        yyval.orderbyitem = new OrderByItem(*yystack.l_mark[0].token, OrderByItem::ASC);
    }
break;
case 55:
#line 222 "sql.y"
	{
        yyval.orderbyitem = new OrderByItem(*yystack.l_mark[-1].token, OrderByItem::ASC);
    }
break;
case 56:
#line 226 "sql.y"
	{
        yyval.orderbyitem = new OrderByItem(*yystack.l_mark[-1].token, OrderByItem::DESC);
    }
break;
#line 876 "yyparser.cpp"
    }
    yystack.s_mark -= yym;
    yystate = *yystack.s_mark;
    yystack.l_mark -= yym;
    yym = yylhs[yyn];
    if (yystate == 0 && yym == 0)
    {
#if YYDEBUG
        if (yydebug)
            printf("%sdebug: after reduction, shifting from state 0 to\
 state %d\n", YYPREFIX, YYFINAL);
#endif
        yystate = YYFINAL;
        *++yystack.s_mark = YYFINAL;
        *++yystack.l_mark = yyval;
        if (yychar < 0)
        {
            if ((yychar = YYLEX) < 0) yychar = 0;
#if YYDEBUG
            if (yydebug)
            {
                yys = 0;
                if (yychar <= YYMAXTOKEN) yys = yyname[yychar];
                if (!yys) yys = "illegal-symbol";
                printf("%sdebug: state %d, reading %d (%s)\n",
                        YYPREFIX, YYFINAL, yychar, yys);
            }
#endif
        }
        if (yychar == 0) goto yyaccept;
        goto yyloop;
    }
    if ((yyn = yygindex[yym]) && (yyn += yystate) >= 0 &&
            yyn <= YYTABLESIZE && yycheck[yyn] == yystate)
        yystate = yytable[yyn];
    else
        yystate = yydgoto[yym];
#if YYDEBUG
    if (yydebug)
        printf("%sdebug: after reduction, shifting from state %d \
to state %d\n", YYPREFIX, *yystack.s_mark, yystate);
#endif
    if (yystack.s_mark >= yystack.s_last && yygrowstack(&yystack))
    {
        goto yyoverflow;
    }
    *++yystack.s_mark = (short) yystate;
    *++yystack.l_mark = yyval;
    goto yyloop;

yyoverflow:
    yyerror("yacc stack overflow");

yyabort:
    yyfreestack(&yystack);
    return (1);

yyaccept:
    yyfreestack(&yystack);
    return (0);
}
